/**
 * snow_bros/snow_bros
 * 雪人兄弟老虎机
 * 
 * @author Zdream
 * @since aurora 0.4.0
 */

import shadyFactory from './shady';

function create(context, key) {
	switch (key) {
		case 'main': return createMain(context);
		case 'shady': return createShadyPromise(context);
		default: return null;
	}
}

function createMain(context) {
  return {
    template : context.template,
    data : function () {
      return {
        status : null,
        probability : [
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        ]
      }
    },
    props : {
      sid : String,
    },
    methods : {
      // 1. 基础部分
      btnRun : function () {
        context.sendCommand(this.sid, 'handlerClick');
      },
      btnStop : function (index) {
        context.sendCommand(this.sid, 'buttonClick', {
          index
        });
      },

      // 2. 音频部分 + 自动移除的配置
      btnAudioEnable : function () {
        context.sendCommand(this.sid, 'trackerEnable', {
          enable : true
        });
      },
      btnAudioDisable : function () {
        context.sendCommand(this.sid, 'trackerEnable', {
          enable : false
        });
      },

      btnAutoRemoveEnable : function () {
        context.sendCommand(this.sid, 'autoRemove', {
          enable : true
        });
      },
      btnAutoRemoveDisable : function () {
        context.sendCommand(this.sid, 'autoRemove', {
          enable : false
        });
      },

      // 3. 摇奖概率
      // 实时计算摇奖概率
      countProbability : function () {
        /** @type {string[]} */
        let confirms = [null, null, null];
        for (let pos = 0; pos < 3; pos++) {
          if (this.status.data.stop[pos]) {
            confirms[pos] = this.status.data.slots[pos];
          }
        }

        for (let pos = 0; pos < 3; pos++) {
          /** @type {number[]} */
          let probabilitys = this.probability[pos];

          if (this.status.data.stop[pos]) {
            let v = this.status.data.slots[pos];
            probabilitys.fill(0);
            probabilitys[v] = 1;
            continue;
          }

          let sum = 0;
          let counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
          this.status.data.candidates.forEach((candidate) => {
            if ((confirms[0] != null && confirms[0] != candidate.slot[0])
                || (confirms[1] != null && confirms[1] != candidate.slot[1])
                || (confirms[2] != null && confirms[2] != candidate.slot[2])) {
              return;
            }

            counts[candidate.slot[pos]] += candidate.weight;
            sum += candidate.weight;
          });

          for (let i = 0; i < counts.length; i++) {
            probabilitys[i] = counts[i] / sum;
          }
        }
      },

      // 监听事件部分
      onScreenUpdated : function () {
        let status = this.status = context.getStatus(this.sid);
        if (status == null) {
          return;
        }
        this.countProbability();
      },
    },
    mounted : function () {
      this.onScreenUpdated();
      
      context.getBus().$on('screen-status-sync-all', this.onScreenUpdated);
      context.getBus().$on('screen-updated', this.onScreenUpdated);
      context.getBus().$on('screen-destroyed', this.onScreenUpdated);
    },
    destroyed : function () {
      context.getBus().$off('screen-status-sync-all', this.onScreenUpdated);
      context.getBus().$off('screen-updated', this.onScreenUpdated);
      context.getBus().$off('screen-destroyed', this.onScreenUpdated);
    }
  }
}

function createShadyPromise(context) {
  return context.fetchHtmlScript('monitor/shady.html').then(html => {
		return shadyFactory(context, html);
	});
}

export default {
	factory : create,
	tabs : [{
		key : 'shady',
		name : '黑幕',
		icon : 'barcode'
	}]
};
